package com.heima.wemedia.listen;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.common.constants.AdminConstants;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author:Dikk
 * @create: 2023-08-02 11:38
 * @Description: ListenToUser
 */
@Component
@Slf4j
public class ListenToUser {

    @Autowired
    private WmUserService wmUserService;

    @KafkaListener(topics = AdminConstants.WEMEDIAL_USER_TOPIC)
    public void createAccount(String message) {
        if (StringUtils.isNotBlank(message)) {
            //解析json
            ApUser apUser = JSONObject.parseObject(message, ApUser.class);
            WmUser wmUser = new WmUser();
            BeanUtils.copyProperties(apUser, wmUser);
            wmUser.setApUserId(apUser.getId());
            wmUser.setId(null);
            wmUser.setStatus(9);
            wmUser.setCreatedTime(new Date());
            wmUserService.save(wmUser);
        }

    }
}
